package com.exam.demo.controller;

import com.exam.demo.pojo.Answer;
import com.exam.demo.pojo.Problem;
import com.exam.demo.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/library")
//用于题库管理的类
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    @RequestMapping("/addproblem")
    //新增问题
    private @ResponseBody int addProblem(@RequestBody Problem problem){
        return this.libraryService.addProblem(problem);
    }

    @RequestMapping("/deleteproblem")
    //删除问题
    private @ResponseBody int deleteProblem(@RequestParam("proNo") int proNo){
        return this.libraryService.deleteProblem(proNo);
    }

    @RequestMapping("/editproblem")
    //修改问题
    private @ResponseBody int editProblem(@RequestBody Problem problem){
        return this.libraryService.editProblem(problem);
    }

    @RequestMapping("/addanswer")
    //新增答案，调用了更新外键的方法
    private @ResponseBody int addAnswer(@RequestBody Answer answer){
        if(this.libraryService.addAnswer(answer) == 1)
            return this.libraryService.updateProblemAdd(answer);
        else return 2;
    }

    @RequestMapping("/deleteanswer")
    //删除答案，调用了更新外键的方法
    private @ResponseBody int deleteAnswer(@RequestParam("ansNo") int ansNo){
        if(this.libraryService.updateProblemDelete(ansNo) == 1)
            return this.libraryService.deleteAnswer(ansNo);
        else return 2;
    }

    @RequestMapping("/editanswer")
    //修改答案
    private @ResponseBody int editAnswer(@RequestBody Answer answer){
        return this.libraryService.editAnswer(answer);
    }

    @RequestMapping("/getproblemlist")
    private @ResponseBody List<Problem> getProblemList(){
        return this.libraryService.getProblemList();
    }

    @RequestMapping("/getprobyno")
    private @ResponseBody Problem getProByProNo(@RequestParam("proNo") int proNo){
        return this.libraryService.getProByProNo(proNo);
    }

    @RequestMapping("/getproblemlistbyteano")
    //根据老师返回题目列表
    private @ResponseBody List<Problem> getProblemListByTeaNo(@RequestParam("teaNo") String teaNo){
        return this.libraryService.getProblemListByTeaNo(teaNo);
    }

    @RequestMapping("/getproblemlistbyprodetail")
    //根据题目内容返回题目列表
    private @ResponseBody List<Problem> getProblemListByProDetail(@RequestParam("proDetail") String proDetail){
        return this.libraryService.getProblemListByProDetail(proDetail);
    }

    @RequestMapping("/getproblemlistbyprokeyw")
    //根据关键词返回题目列表
    private @ResponseBody List<Problem> getProblemListByProKeyw(@RequestParam("proKeyw") String proKeyw){
        return this.libraryService.getProblemListByProKeyw(proKeyw);
    }

    @RequestMapping("/getproblemlistbyprotype")
    //根据题目类型返回题目列表
    private @ResponseBody List<Problem> getProblemListByProType(@RequestParam("proType") String proType){
        return this.libraryService.getProblemListByProType(proType);
    }

    @RequestMapping("/getproblemlistbychano")
    //根据题目章节号返回题目列表
    private @ResponseBody List<Problem> getProblemListByChaNo(@RequestParam("chaNo") int chaNo){
        return this.libraryService.getProblemListByChaNo(chaNo);
    }

    @RequestMapping("/getanswerbyproansno")
    //根据题目返回题目答案
    private @ResponseBody Answer getAnswerByProAnsno(@RequestParam("proAnsno") int proAnsno){
        return this.libraryService.getAnswerByProAnsno(proAnsno);
    }
    @RequestMapping("/getansnobyprono")
    //根据题目返回题目答案
    private @ResponseBody int getAnsnoByProNo(@RequestParam("proNo") int proNo){
        return this.libraryService.getAnsnoByProNo(proNo);
    }
}
